$(function () {
    var $menuTree = $("#menuTreeList"),
        $menuTable = $("#menuTable"),
        $menuMenu = $('#menuMenu'),
        $dialogMenuAdd = $("#dialog-menu-edit"),
        $windowMenuAddBtns = $("#window-menu-add-btns"),
        $menuForm = $("#menu-form");
    var timeFn;

    $menuTree.tree({
        url: _ctx + "/sysMenu/manage/list",
        animate: true,
        idField: 'id',
        textField: 'menuName',
        parentField: 'parentId',
        iconField: 'menuIcon',
        dnd: true,
        cache: false,
        onContextMenu: function (e, node) {
            e.preventDefault();
            // select the node
            $menuTree.tree('select', node.target);
            // display context menu
            $menuMenu.menu('show', {
                left: e.pageX,
                top: e.pageY,
                onClick: function (item) {
                    if (item.id === "menu-add") {
                        $menuForm.form("clear");
                        $("#parentId").val(node.id);
                        $dialogMenuAdd.dialog({
                            title: "新增",
                            iconCls: "icon-add"
                        });
                        $dialogMenuAdd.dialog("open");
                    }
                    if (item.id === "menu-add-btns") {
                        $windowMenuAddBtns.window({
                            title: "[<font color='blue'>" + node.menuName + "</font>]的功能按钮"
                        });
                        $("#all-btns").datalist({
                            url: _ctx + "/sysFuncs/allBtns",
                            valueField: 'id',
                            textField: 'funcName',
                            queryParams: {
                                menuId: node.id
                            },
                            textFormatter: function (value, row, index) {
                                return "<span style='padding-left: 40px;'>" + value + "</span>";
                            }
                        });
                        $("#current-btns").datalist({
                            url: _ctx + "/sysFuncs/currentBtns",
                            valueField: 'id',
                            textField: 'funcName',
                            queryParams: {
                                menuId: node.id
                            },
                            textFormatter: function (value, row, index) {
                                return "<span style='padding-left: 40px;'>" + value + "</span>";
                            }
                        });
                        $windowMenuAddBtns.window("open");
                    }
                    if (item.id === "menu-remove") {
                        $.messager.confirm("提示", "确定删除此菜单(按钮) ?", function (r) {
                            if (r) {
                                $.post(_ctx + "/sysMenu/delete", {id: node.id}, function (result) {
                                    if (result.success) {
                                        $.messager.alert("提示", result.message, "info", function () {
                                            $menuTree.tree("reload");
                                            $menuTable.datagrid("reload");
                                        });
                                    } else {
                                        $.messager.alert("提示", result.message, "warning");
                                    }
                                }, "json");
                            }
                        });
                    }
                    if (item.id === "menu-edit") {
                        $menuForm.form("load", node);
                        $dialogMenuAdd.dialog({
                            title: "修改",
                            iconCls: "icon-pencil"
                        });
                        $dialogMenuAdd.dialog("open");
                    }
                }
            });
        },
        onDblClick: function (node) {
            clearTimeout(timeFn);
            $menuTree.tree("toggle", node.target);
        },
        onClick: function (node) {
            clearTimeout(timeFn);
            timeFn = setTimeout(function () {
                if (!(node.children && node.children.length > 0)) {
                    $menuTable.datagrid("reload", {menuId: node.id});
                }
            }, 200); // 延时时长设置
        },
        onDrop: function (target, source) {
            var id = source.id; // 将要修改的菜单id
            var parentId = $menuTree.tree('getNode', target).id;
            $.post(_ctx + "/sysMenu/update", {id: id, parentId: parentId}, function (result) {
                if (result.success) {
                    // $menuTree.tree("reload");
                    $.messager.alert("提示", result.message, "info");
                } else {
                    $.messager.alert("提示", result.message, "warning");
                }
            }, "json");
        }
    });

    $menuTable.datagrid({
        rownumbers: true,
        toolbar: "#datagirdToolbar",
        /*        pageSize: 10,
                pagination: true,
                pageList: [5, 10, 20, 50, 100],*/
        multiSort: true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        url: _ctx + "/sysMenuFunc/table/list",
        fit: true,
        border: false,
        fitColumns: true,
        frozenColumns: [[
            {field: '', title: '', checkbox: true},
            {field: 'id', title: '标识', width: 100}
        ]],
        columns: [[
            {field: 'funcName', title: '按钮名称', width: 100},
            {field: 'funcIcon', title: '按钮图标', width: 100},
            {field: 'permissions', title: '权限代码', width: 100}
        ]]
    });

    $dialogMenuAdd.dialog({
        buttons: [{
            text: '确定',
            iconCls: 'icon-ok',
            handler: function () {
                var r = $menuForm.form("validate");
                if (r) {
                    var menuData = $menuForm.serializeArray();
                    $.post(_ctx + "/sysMenu/saveOrUpdate", menuData, function (result) {
                        if (result.success) {
                            $dialogMenuAdd.dialog('close');
                            $.messager.alert("提示", result.message, "info", function () {
                                $menuTree.tree("reload");
                            });
                        } else {
                            $.messager.alert("提示", result.message, "warning");
                        }
                    }, "json");
                }
            }
        }, {
            text: '取消',
            iconCls: 'icon-cancel',
            handler: function () {
                $dialogMenuAdd.dialog('close');
            }
        }]
    });

    $("#toLeft").click(function () {
        var funcSelected = $("#current-btns").datalist("getSelected");
        var menuSelected = $menuTree.tree("getSelected");
        if (funcSelected && menuSelected) {
            $.post(_ctx + "/sysMenuFunc/delete", {funcId: funcSelected.id, menuId: menuSelected.id}, function (result) {
                if (result.success) {
                    $("#all-btns").datalist("reload");
                    $("#current-btns").datalist("reload");
                } else {
                    $.messager.alert("提示", result.message, "warning");
                }
            }, "json");
        }
    });

    $("#toRight").click(function () {
        var funcSelected = $("#all-btns").datalist("getSelected");
        var menuSelected = $menuTree.tree("getSelected");
        if (funcSelected && menuSelected) {
            $.post(_ctx + "/sysMenuFunc/insert", {funcId: funcSelected.id, menuId: menuSelected.id}, function (result) {
                if (result.success) {
                    $("#all-btns").datalist("reload");
                    $("#current-btns").datalist("reload");
                } else {
                    $.messager.alert("提示", result.message, "warning");
                }
            }, "json");
        }
    });

});
